package top.meeting.room.module.system.controllers;


import com.jfinal.aop.Before;
import com.jfinal.ext.interceptor.GET;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jfinal.log.Log4jLog;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import top.jfinal.jwtTokenPlugin.annotation.Auth;
import top.meeting.room.common.base.BaseController;
import top.meeting.room.module.system.form.RoleForm;
import top.meeting.room.module.system.model.Role;
import top.meeting.room.module.system.query.RoleQuery;
import top.meeting.room.module.system.services.impl.RoleService;

import java.util.Arrays;

/**
 * 我有故事，你有酒么？
 * JKhaled created by yunqisong@foxmail.com 2017/7/20
 * FOR : 角色API控制
 */
@Auth(hasForces = {"角色管理", "所有权限"})
public class RoleController extends BaseController {

    private static final Log _log = Log4jLog.getLog(RoleController.class);

    /**
     * 保存
     */
    @Before({POST.class, Tx.class})
    public void save() {
        RoleForm roleForm = getJsonParaToObj(RoleForm.class);
        _log.debug("Save role by roleForm : {}" + String.valueOf(roleForm));
        Role role = RoleService.me.saveAsModel(roleForm);
        if (role != null)
            renderJsonSuccess(role);
        else
            renderJsonError("保存角色异常");
    }

    /**
     * 删除 ID
     */
    @Before({GET.class, Tx.class})
    public void delete() {
        Long id = getParaToLong();
        _log.debug("Delete role by id : {}" + String.valueOf(id));
        if (id != null) {
            RoleService.me.deleteById(id);
            renderJsonSuccess();
        } else
            renderJsonError("删除的ID不可以为空");
    }

    /**
     * 删除 IDS
     */
    @Before({GET.class, Tx.class})
    public void deletes() {
        String ids = getPara();
        _log.debug("Delete role by ids : {}" + String.valueOf(ids));
        if (StrKit.notBlank(ids)) {
            RoleService.me.deleteByIds(ids);
            renderJsonSuccess();
        } else
            renderJsonError("删除的ID不可以全为空");
    }

    /**
     * 查询 列表
     */
    @Before({GET.class})
    public void find() {
        RoleQuery roleQuery = getUrlParaToObj(RoleQuery.class);
        _log.debug("Find page<Record(role)> by roleQuery : {}" + String.valueOf(roleQuery));
        Page<Record> retVal = RoleService.me.pageRecord(roleQuery);
        if (retVal != null)
            renderJsonSuccess(retVal);
        else
            renderJsonSuccess(Arrays.asList());
    }

    /**
     * 查询 个体
     */
    @Before({GET.class})
    public void get() {
        Long id = getParaToLong();
        _log.debug("Get role by id : {}" + String.valueOf(id));
        if (id != null) {
            renderJsonSuccess(RoleService.me.getModel(id));
        } else
            renderJsonError("查询的ID不可以为空");
    }

    /**
     * 查询所有角色
     */
    @Before({GET.class})
    @Auth(hasForces = {"用户管理"})
    public void allRoles() {
        _log.debug("Find all roles: {}");
        Page<Record> rolePage = RoleService.me.pageRecord(new RoleQuery(null, 1, Integer.MAX_VALUE));
        if (rolePage != null && rolePage.getList() != null && rolePage.getList().size() > 0)
            renderJsonSuccess(rolePage.getList());
        else
            renderJsonError("查询不到任何角色");
    }
}
